<!-- 贡献分明细 -->
<template>
  <div style="padding-left:20px;">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input type="number" v-model="data.mobile" size="medium" class="iup_width200" placeholder="请输入手机号"></el-input>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="data.startTime" value-format="yyyy-MM-dd" type="date" size="medium" :clearable="false" :editable="false"
          placeholder="选择开始日期" class="iup_width200"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="data.endTime" value-format="yyyy-MM-dd" type="date" size="medium" :clearable="false" :editable="false"
          placeholder="选择结束日期" class="iup_width200"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-select v-model="data.level" size="medium" class="iup_width200" placeholder="请选择">
          <el-option v-for="item in levelList" :key="item.code" :label="item.name" :value="item.code"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="medium" @click="search">查询</el-button>
        <el-button type="info" size="medium" @click="reset">重置</el-button>
        <el-button type="danger" size="medium" @click="exportExcel">导出</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="devoteList" empty-text border stripe v-loading="loading" element-loading-text="数据加载中">
      <el-table-column type="index" label="序号" width="70"></el-table-column>
      <el-table-column prop="mobile" label="手机号" align="center"></el-table-column>
      <el-table-column prop="memberLevel" label="会员等级" align="center"></el-table-column>
      <el-table-column prop="projectName" label="项目名称" align="center"></el-table-column>
      <el-table-column prop="mark" label="贡献分变化" align="center">
        <template slot-scope="scope">
          <span class="green">{{common.formatAmount(scope.row.mark,0)}}</span>
        </template>
      </el-table-column>

      <el-table-column prop="tradeTime" label="贡献时间" :formatter="common.formatTime" align="center"></el-table-column>
      <el-table-column prop="remark" label="备注" align="center"></el-table-column>
    </el-table>
    <!-- 分页开始 -->
    <pagination :total="total" :pageData="data" @hanleSize="changePage" @hanlePage="handleSizeChange"></pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loading: false,
      data: {
        pageSize: 10,
        currentPage: 1,
        mobile: "",
        projectNo: "",
        startTime: "",
        endTime: "",
        level: ""
      },
      total: 0,
      devoteList: [], //余额明细列表
      levelList: [] //会员等级
    };
  },
  methods: {
    //点击页数触发
    changePage (page) {
      this.data.currentPage = page;
      this.getDevoteList();
    },
    //选择页数最多显示几条数据
    handleSizeChange (val) {
      this.data.pageSize = val;
      this.getDevoteList();
    },
    // 获取字典表
    async getterm () {
      this.levelList = await this.common.enumsList("USER_LEVEL");
    },
    async getDevoteList () {
      this.loading = true;
      const url = "/devote/queryPage";
      const result = await this.request.httpRequest("POST", url, this.data);
      if (result.code == 200) {
        this.total = result.data.totalRows;
        this.data.pageSize = result.data.pageSize;
        this.data.currentPage = result.data.currentPage;
        this.devoteList = result.data.datas;
        this.loading = false;
      } else {
        this.loading = false;
        this.$message({ type: "error", message: result.msg });
      }
    },
    reset () {
      Object.assign(this.$data.data, this.$options.data().data);
      this.getDevoteList();
    },
    search () {
      if (this.data.endTime < this.data.startTime && !this.data.endTime == "") {
        this.$message({ type: "error", message: "开始时间不能大于结束时间" });
        return;
      }
      if (this.data.startTime == "" && this.data.endTime) {
        this.$message({ type: "error", message: "开始时间不能为空" });
        return;
      } else if (this.data.endTime == "" && this.data.startTime) {
        this.$message({ type: "error", message: "结束时间不能为空" });
        return;
      }
      this.data.currentPage = 1;
      this.getDevoteList();
    },
    // 导出excel
    async exportExcel () {
      if (this.data.endTime < this.data.startTime && !this.data.endTime == "") {
        this.$message({ type: "error", message: "开始时间不能大于结束时间" });
        return;
      }
      if (this.data.startTime == "" && this.data.endTime) {
        this.$message({ type: "error", message: "开始时间不能为空" });
        return;
      } else if (this.data.endTime == "" && this.data.startTime) {
        this.$message({ type: "error", message: "结束时间不能为空" });
        return;
      }
      let onTime = new Date(this.data.startTime)
      let startTime = onTime.getTime(onTime)
      let offTime = new Date(this.data.endTime)
      let endTime = offTime.getTime(offTime)
      let Threedays = 1000 * 60 * 60 * 24 * 3
      if (endTime - startTime >= Threedays) {
        this.$message({ type: "error", message: "开始时间与结束时间间隔不能大于3天" });
        return;
      }
      if (this.devoteList.length === 0) {
        this.$message({ type: "error", message: '当前没有可导出的数据' });
        return
      }
      const url = "/devote/export";
      const result = await this.request.httpRequest("POST", url, this.data,'blob');
			if(result){
				this.common.exportExcel(result, '贡献分明细')
			}
      
    },
  },
  mounted () {
    Promise.all([this.getterm(), this.getDevoteList()]);
  }
};
</script>
<style scoped>
.el-form {
  padding: 15px 0px !important;
}
.el-form-item {
  margin-bottom: 0px !important;
}
</style>
